package com.nt.train.controller;

import com.nt.train.config.properties.Person;
import com.nt.train.config.properties.Person2;
import com.nt.train.config.properties.Person3;
import com.nt.train.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 03-配置文件演示1
 * @author hlf
 * @version 1.0
 * @date 2022/8/9 13:23
 */
@Slf4j
@RestController
@RequestMapping("/properties")
public class PropertiesController {

    @Autowired
    Person person;

    @Autowired
    Person2 person2;

    @Autowired
    Person3 person3;

    /**
     *  @Value("${name}")
     *  // @Value("${MAVEN_HOME}" )
    //@Value("${server.web.domain:test}")
     @Value("${server.web.domain:#{null}}")
     * 但是，如果配置文件中没有设置name的值，spring在启动的时候会报错。这时需要给name配置默认值，代码如
     */
    @Value("${name:bob}")
    private String name;

    @Value("${age:18}")
    private int age;

    @Value("#{'${server.id:127.0.0.1,192.168.1.18}'.split(',')}")
    private List<String> serverId1;

    @Value("${server.id:127.0.0.1,192.168.1.18}")
    private List<String> serverId2;

    @Value("${server.id:127.0.0.1,192.168.1.18}")
    private String[] serverId3;

    @Value("${fastdfs.cache.redis.expireTime:86400}")
    public static int EXPIRE_TIME;


    @RequestMapping(value = "/read", method = RequestMethod.POST)
    public ResultVo read(){
        try {
            log.info("测试打印日志处理");
            return ResultVo.ok(person);
        } catch (Exception e) {
            return ResultVo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/read2", method = RequestMethod.POST)
    public ResultVo read2(){
        try {
            log.info("测试打印日志处理");
            return ResultVo.ok(person2);
        } catch (Exception e) {
            return ResultVo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/read3", method = RequestMethod.POST)
    public ResultVo read3(){
        try {
            log.info("测试打印日志处理");
            return ResultVo.ok(person3);
        } catch (Exception e) {
            return ResultVo.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/read4", method = RequestMethod.POST)
    public ResultVo read4(){
        try {
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("name",name);
            resultMap.put("age",age);
            resultMap.put("serverId1",serverId1);
            resultMap.put("serverId2",serverId2);
            resultMap.put("serverId3",serverId3);
            resultMap.put("EXPIRE_TIME",EXPIRE_TIME);
            return ResultVo.ok(resultMap);
        } catch (Exception e) {
            return ResultVo.error(e.getMessage());
        }
    }
}
